clear
%% PROBLEM
d = 2;
x_bound = repmat([0,1],[d,1]);
k = [4,4];
solutionId = fullfact(k);
OptSet.sol = (2.*solutionId-1)./2./repmat(k,[prod(k),1]);
OptSet.fval = d;

rep = 1;

Problem.Dimension = d; % the dimension of the decision variable
Problem.LowerBound = x_bound(:,1)'; % the lower bound of the decision variable
Problem.UpperBound = x_bound(:,2)'; % the upper bound of the decision variable
Problem.NumObjFunc = 2; % the number of objective functons
Problem.ObjFunc = @(x)ModifiedObj(@ObjValue,x);  % the objective function
%% ALGORITHM
Algorithm.PopulationSize = 160;  % the population size of the ga
% Algorithm.StopCriteria = [1,10000];  % stop when the budget is exhausted
Algorithm.StopCriteria = [2,100];  % stop when the number of generation is reached
% the followings are not necessary
Algorithm.pc = 0.9;  % the crossover probability
Algorithm.pm = 0.5;  % the mutation probability
Algorithmeta_c = 10;  % SBX index
Algorithmeta_m =20;  % mutation index
Algorithm.ParetoType = [2, 0.1, 0.1];
[ParetoFront, ParetoSet, GenerationNum, EvaluationNum] = nsga2_multimodal(Problem, Algorithm);
GenerationNum
EvaluationNum = EvaluationNum*(2*d+1)

%% FIGURES
if rep == 1
    if d==1
        x=x_bound(1,1):(x_bound(1,2)-x_bound(1,1))/1000:x_bound(1,2);
        z=ObjValue(x');
        z_max=max(z);
        z_min=min(z);
        figure()
        hold on
        plot(x,z)
        plot(ParetoSet,ParetoFront(:,1),'.')
        hold off
    end
    if d == 2
        figure()
        hold on
        scatter(ParetoSet(:,1),ParetoSet(:,2),2,ParetoFront(:,1),'filled');
        plot(OptSet.sol(:,1),OptSet.sol(:,2),'o')
        hold off
        xlabel('','Interpreter','latex','String','$x_1$')
        ylabel('','Interpreter','latex','String','$x_2$')
        set(gca,'Fontname','Times New Roman','FontSize',16);
    end
end
